<template>
	<view class="page">
		<u-navbar :border-bottom="false" :is-fixed="true" back-icon-color="#fff" title-color="#fff"
			:background="background" title="一键匹配">

		</u-navbar>
		<xingqiu v-if="isShow&&!isChoose" style="margin-top: 100rpx;" ref="tag" :speed="speed" :texts="texts"></xingqiu>
		<view v-else>
			<view style="margin-top: 50%;">
				<view class="avatar" v-if="isChoose">
					<image class="userhead" :src="matchUser.avatar" mode="aspectFit"></image>
					<view class="username">{{matchUser.nickname}}</view>
				</view>
				<view class="unchoosed-wrap" v-else>
					<image class="unc-img" src="/static/images/pay/s1.png" mode="aspectFit"></image>
					<image class="unc-img" src="/static/images/pay/s2.png" mode="aspectFit"></image>
				</view>
				<view class="inner"></view>
				<view class="inner outter"></view>
				<view class="inner outter1"></view>
				<view class="inner outter2"></view>
			</view>
		</view>


		<view class="search-btn-wrap">
			<view v-if="isShow||isChoose" style="color: #fff; text-align: center; line-height: 36px;">
				{{isChoose?'匹配成功':'匹配中…'}}
			</view>
			<view v-if="!isChoose&&!isShow">
				<view class="btn btn-plain" @click="navTo()">设置条件</view>
				<view class="btn" @click="startSearch()">开始匹配</view>
			</view>
			<view v-if="isChoose&&!isShow" class="btn" @click="bindConfirmInvite()">聊一聊</view>
		</view>

	</view>
</template>

<script>
	const app = getApp();
	import xingqiu from '@/components/xingqiu/xingqiu.vue'
	import {
		matchingCreateOrder,
		matching
	} from "@/common/config/chat.js"
	export default {
		components: {
			xingqiu
		},
		data() {
			return {
				isShow: false,
				texts: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
				speed: 3, //越大越快
				background: {
					backgroundColor: 'rgba(0,0,0,0)',
				},
				userID: '',
				searchUser: {},
				isChoose: false,
				matchUser: {},
				form: ''
			}
		},
		onBackPress() {
			this.$refs.tag.clearTimer();
		},
		onLoad(e) {
			if (e.pay == 'payok') {
				this.form = e.form
				this.set()
				setTimeout(()=>{
					console.log(e.orderId)
					this.seach(e.orderId)
				},1000)
			}
		},
		methods: {
			navTo() {
				uni.navigateTo({
					url: '/pages/Search/setting'
				})
			},
			// back() {
			// 	//@zxyuns 处理兼容，如果没有上一级界面则返回首页
			// 	const pages = getCurrentPages();
			// 	if (pages.length === 2) {
			// 		uni.navigateBack({
			// 			delta: 1
			// 		});
			// 	} else if (pages.length === 1) {
			// 		uni.switchTab({
			// 			url: '/pages/index/index',
			// 		})
			// 	} else {
			// 		uni.navigateBack({
			// 			delta: 1
			// 		});
			// 	}
			// },
			getuserProfile() {
				uni.$TUIKit.getUserProfile({
					userIDList: [this.userID]
				}).then(imRes => {
					console.log('获取腾讯云用户信息', imRes)
					if (imRes.data.length > 0) {
						this.setData({
							searchUser: imRes.data[0]
						});
			  } else {
						uni.showToast({
							title: '用户不存在',
							icon: 'error'
						});
						this.setData({
							userID: ''
						});
					}
				});
			},
			// 开始匹配
			startSearch() {
				// 选中匹配对象
				this.set({
					content: ''
				})
			},
			seach(data) {
				// const order_no = res.data.data.order_no
				let order_no = data
				matching({
					order_no
				}).then(res => {
					console.log('匹配结果',res.data)
					this.matchUser = res.data.data
					this.userID = String(res.data.data.id)

					this.isChoose = !this.isChoose
					this.isShow = false

					// 查找 TUI 聊天用户信息
					this.getuserProfile()
				})
			},
			set(data) {
				if (this.isShow) return
				this.isShow = true
				setTimeout(()=>{
					this.speed=2
				},1500)
				setTimeout(() => {
					matchingCreateOrder(data).then(res => {
						this.seach(res.data.data.order_no)
					})
				}, 2500)

			},
			bindConfirmInvite() {
				console.log(this.searchUser.userID, 'this.searchUser.userID')
				uni.redirectTo({
					url: `/pages/TUI-Chat/chat?conversationID=C2C${this.searchUser.userID}`
				});
			}
		}
	}
</script>

<style scoped>
	.page {
		height: 100vh;
		/* background: #12121e; */
		background-size: 100% 100%;
		background-image: url(../../static/bg.png);
	}

	.search-btn-wrap {
		width: 100%;
		position: fixed;
		bottom: 75rpx;
		left: 0;
		z-index: 999;
	}

	.search-btn-wrap .btn {
		text-align: center;
		padding: 30rpx;
		border-radius: 300rpx;
		width: 557rpx;
		margin: 54rpx auto;
		background: #fff;
		color: #FA9700;
		border: 1px solid #FA9700;
		;
		font-size: 30rpx;
	}

	.search-btn-wrap .btn.btn-plain {
		background: none;
		color: #FFFFFF;
		border: 1px solid #FFFFFF;
		;
	}
</style>
<style lang="scss">
	.inner {
		width: 100rpx;
		height: 100rpx;
		background: #aabdaf;
		border-radius: 50%;
		position: fixed;
		left: 50%;
		margin-left: -50rpx;
		margin-top: 150rpx;
		z-index: 10;
		animation-duration: 2s;
		-webkit-animation-name: state1;
		animation-name: state1;
		-webkit-animation-timing-function: linear;
		animation-timing-function: linear;
		-webkit-animation-iteration-count: infinite;
		animation-iteration-count: infinite;
		opacity: 0;
	}

	@keyframes state1 {
		0% {
			opacity: .5;
			-webkit-transform: scale(1);
			transform: scale(1);
		}

		100% {
			opacity: 0;
			-webkit-transform: scale(8);
			transform: scale(8);
		}
	}

	.unchoosed-wrap {
		position: fixed;
		left: 50%;
		transform: translateX(-50%);
		margin-top: 60rpx;
		border-radius: 100%;
		width: 300rpx;
		height: 300rpx;
		background-color: rgba(255, 255, 255, .9);
		z-index: 11;
	}

	.unchoosed-wrap::before {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		content: '';
		width: 200rpx;
		height: 200rpx;
		border-radius: 300rpx;
		border: 1px solid #FA9700;
	}

	.unc-img {
		width: 134rpx;
		height: 134rpx;
		position: absolute;
	}

	.unc-img:nth-child(1) {
		top: 50rpx;
		left: 10rpx;
	}

	.unc-img:nth-child(2) {
		bottom: 30rpx;
		right: 0rpx;
	}

	.avatar {
		position: fixed;
		left: 50%;
		transform: translateX(-50%);
		margin-top: 60rpx;
		border-radius: 100%;
		width: 281rpx;
		height: 281rpx;
		background-color: #a4b6a9;
		z-index: 11;
	}

	.avatar {
		.userhead {
			width: 100%;
			height: 100%;
			border-radius: 100%;
		}

		.username {
			font-size: 28rpx;
			color: #333333;
			width: 90%;
			line-height: 74rpx;
			text-align: center;
			border-radius: 74rpx;
			background: #fff;
			box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16);
			position: absolute;
			left: 5%;
			bottom: -40rpx;
		}
	}

	.outter {
		animation-delay: 0.5s;
	}

	.outter1 {
		animation-delay: 1s;
	}

	.outter2 {
		animation-delay: 1.5s;
	}
</style>
